package com.nstar.components.graphicinfo
{
	import com.nstar.base.event.RequestEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.utils.ColorUtil;
	
	public class VariableText extends EventDispatcher
	{
		/**
		 * 文本对象显示的属性，包括颜色和字体大小
		 * 
		 * 数字，起始值，结束值，结束值即为最终要显示的数字
		 */ 
		 
		public static const NUMBER_CHANGED:String = "numberChanged"; 
		 
		public var color:uint = 0xFF0000;//字体颜色
		public var fontSize:int = 10;
		public var step:int = 10;//数字递增的步长
		public var duration:int = 10;//数字变化的时间间隔
		
		public var currentNumber:int = 0 ;//当前显示的数字大小，默认从0开始
		private var number:int;//要显示的数字
		
		private var startSize:int = 12; //字体开始大小
		private var endSize:int = 48;//显示的最大字体

		private var timer:Timer;
		private var fontStep:Number;//字体大小变化步长
		
		private var bright:int = -255;
		private var colorStep:Number;//颜色变化步长
		public var currentColor:uint;//当前颜色值
		
		public function VariableText(value:int,startSize:int,endSize:int,color:Number,step:Number)
		{
			this.number = value;
			timer = new Timer(duration);
			timer.addEventListener(TimerEvent.TIMER,countHandler);
			fontSize = startSize;
			this.startSize = startSize;
			this.endSize = endSize;
			this.color = color;
			
			if(step<1) step = 1;
			
			this.step = step;
			
			//计算字体大小变化的步长
			fontStep = (endSize-startSize)/(number/step);
			colorStep = 255/(number/step);
			//trace("fontStep="+fontStep+",colorStep="+colorStep+",step="+step);
		}
		
		private function countHandler(event:TimerEvent):void{
			next();
		}
		
		//下一个值，同时计算字体大小和颜色
		private function next():void{
			currentNumber += step;
			fontSize += fontStep;
			bright += colorStep;
			currentColor = ColorUtil.adjustBrightness(color,bright);
			if(currentNumber>=number){
				currentNumber = number;
				startSize = endSize;
				currentColor = color;
				stop();
			}
			this.dispatchEvent(new RequestEvent(NUMBER_CHANGED,this));
		}
		
		//开始变化
		public function start():void{
			timer.start();
		}
		
		public function stop():void{
			timer.stop();
			timer.reset();
			//trace("VariableText stoped!");
		}
		
		override public function toString():String{
			return this.startSize+","+this.endSize+","+this.step;
		}

	}
}